/**
 * 购物车服务类
 * 用于购物车相关操作，包括添加商品到购物车、获取购物车列表等
 */

const request = require('../libs/request');

class CartService {
	/**
	 * 添加商品到购物车
	 * @param {Object} params 添加参数
	 * @param {number} params.goods_id 商品ID
	 * @param {string} params.spec 商品规格（格式如：红色@XL@棉质）
	 * @param {number} params.quantity 商品数量
	 * @param {number} params.user_id 用户ID
	 * @returns {Promise}
	 */
	static async addToCart(params = {}) {
		const { goods_id, specs, quantity = 1,sku_id} = params;
		
		if (!goods_id) {
			return {
				success: false,
				data: null,
				message: '商品ID不能为空'
			};
		}

		if (!specs) {
			return {
				success: false,
				data: null,
				message: '商品规格不能为空'
			};
		}

		if (quantity <= 0) {
			return {
				success: false,
				data: null,
				message: '商品数量必须大于0'
			};
		}

		try {
			const response = await request.post('/v1/cart/add', {
                    goods_id,
					specs,
                    sku_id,
					quantity,
                },
                {
                    showLoading: false,
                    needAuth: true,
                }
			);

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '添加到购物车成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '添加到购物车失败'
			};
		}
	}

	/**
	 * 获取购物车列表
	 * @param {Object} params 查询参数
	 * @param {number} params.user_id 用户ID
	 * @returns {Promise}
	 */
	static async getCartList(params = {}) {
		

		try {
			const response = await request.get('/v1/cart/list', {
				needAuth: true,
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '获取购物车列表成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取购物车列表失败'
			};
		}
	}

	/**
	 * 更新购物车商品数量
	 * @param {Object} params 更新参数
	 * @param {number} params.cart_id 购物车记录ID
	 * @param {number} params.quantity 新的商品数量
	 * @returns {Promise}
	 */
	static async updateQuantity(params = {}) {
		const { cart_id, quantity } = params;
		
		if (!cart_id) {
			return {
				success: false,
				data: null,
				message: '购物车记录ID不能为空'
			};
		}

		if (quantity <= 0) {
			return {
				success: false,
				data: null,
				message: '商品数量必须大于0'
			};
		}

		try {
			const response = await request.post('/v1/cart/update-quantity', {
				needAuth: true,
				data: {
					cart_id,
					quantity
				}
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '更新商品数量成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '更新商品数量失败'
			};
		}
	}

	/**
	 * 删除购物车商品
	 * @param {Object} params 删除参数
	 * @param {number} params.cart_id 购物车记录ID
	 * @returns {Promise}
	 */
	static async removeFromCart(params = {}) {
		const { cart_id } = params;
		
		if (!cart_id) {
			return {
				success: false,
				data: null,
				message: '购物车记录ID不能为空'
			};
		}

		try {
			const response = await request.post('/v1/cart/remove', {
				needAuth: true,
				data: { cart_id }
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '删除购物车商品成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '删除购物车商品失败'
			};
		}
	}

	/**
	 * 清空购物车
	 * @param {Object} params 清空参数
	 * @param {number} params.user_id 用户ID
	 * @returns {Promise}
	 */
	static async clearCart(params = {}) {
		const { user_id } = params;
		
		if (!user_id) {
			return {
				success: false,
				data: null,
				message: '用户ID不能为空'
			};
		}

		try {
			const response = await request.post('/v1/cart/clear', {
				needAuth: true,
				data: { user_id }
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '清空购物车成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '清空购物车失败'
			};
		}
	}

	/**
	 * 获取购物车商品总数
	 * @param {Object} params 查询参数
	 * @param {number} params.user_id 用户ID
	 * @returns {Promise}
	 */
	static async getCartCount(params = {}) {
		const { user_id } = params;
		
		if (!user_id) {
			return {
				success: false,
				data: null,
				message: '用户ID不能为空'
			};
		}

		try {
			const response = await request.get('/v1/cart/count', {
				needAuth: true,
				data: { user_id }
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '获取购物车商品总数成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取购物车商品总数失败'
			};
		}
	}

	static async submit(data){
		try  {
			const response = await request.post('/v1/cart/submit', data,{
				needAuth: true,
				showErrorInfo: true,
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '获取购物车商品总数成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取购物车商品总数失败'
			};
		}
	}
}

module.exports = CartService;
